package thread;

import java.util.Scanner;

class Music{
    public static boolean isPlay = false;//音乐是否播放
    public static Integer now = -1;//当前音乐播放进度
    public static boolean status = false;//音乐播放状态

}
public class Demo3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Thread t = null;
        while (true){
            System.out.println("请输入选项 1.播放音乐 2.暂停/播放音乐 3.关闭音乐 4.下一首歌 -->");
            Integer i = scanner.nextInt();
            switch (i){
                case 1:
                    System.out.println("请输入音乐播放时长:");
                    t = play(scanner);
                    t.start();
                    break;
                case 2:
                    Music.status = !Music.status;
                    break;
                case 3:
                    Music.now = -1;
                    break;
                case 4:
                    Music.now = -1;
                    System.out.print("请输入音乐播放时长:");
                    t = play(scanner);
                    t.start();
                    break;
            }
        }

    }

    private static Thread play(Scanner scanner) {
        Music.now = scanner.nextInt();
        Music.isPlay = true;
        Music.status = true;
        return new Thread(()->{
            Integer now = Music.now;
            while(Music.isPlay){
                if (Music.status){
                    System.out.println("音乐播放中  --->" + now);
                    now--;
                    if (Music.now <= -1){
                        Music.isPlay = false;
                        System.out.println("音乐播放完毕,线程终止运行");
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }else {
                    System.out.println("音乐暂停中");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
    }
}
